package com.invengo.module_robot.data.repsitory;

import com.invengo.base.http.RobotBaseBean;
import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_robot.ActivitBean;
import com.invengo.library_db.entity.module_robot.RobotTokenBean;
import com.invengo.module_robot.data.entity.ConfigBean;
import com.invengo.module_robot.data.entity.DouBanBeans;
import com.invengo.module_robot.data.entity.PopularBean;
import com.invengo.module_robot.data.entity.SearchBean;
import com.invengo.module_robot.data.repsitory.source.RobotSource;
import com.invengo.module_robot.data.repsitory.source.impl.HttpRobotSourceImpl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import io.reactivex.Observable;
import java8.util.stream.Stream;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.bus.SourceUtils;
import me.goldze.mvvmhabit.http.BaseBean;
import okhttp3.ResponseBody;

public class RobotRepsitory extends BaseModel<RobotSource> implements RobotSource {
    private volatile static RobotRepsitory INSTANCE = null;

    public static RobotRepsitory getInstance() {
        if (INSTANCE == null) {
            synchronized (RobotRepsitory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RobotRepsitory();
                }
            }
        }
        return INSTANCE;
    }
    @Override
    public void refreshSource() {
        source = SourceUtils.getSource(HttpRobotSourceImpl.class, HttpRobotSourceImpl.class);
    }

    @Override
    public Observable<RobotBaseBean<SearchBean>> searchBooks(String keyWord) {
        String encode=keyWord;
        try {
             encode = URLEncoder.encode(keyWord, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return source.searchBooks(encode);
    }

    @Override
    public Observable<RobotBaseBean<SearchBean>> getBookInfoByIsbn(String isbn) {
        return source.getBookInfoByIsbn(isbn);
    }


    @Override
    public Observable<RobotBaseBean<RobotTokenBean>> login(String UserNameOrEmailAddress, String Password) {
        return source.login(UserNameOrEmailAddress,Password);
    }

    @Override
    public Observable<RobotBaseBean<ActivitBean>> getActivities() {
        return source.getActivities();
    }

    @Override
    public Observable<ResponseBody> getActivityImage(String attachmentId) {
        return source.getActivityImage(attachmentId);
    }

    @Override
    public Observable<RobotBaseBean<List<PopularBean>>> getPopularBook() {
        return source.getPopularBook();
    }

    @Override
    public Observable<RobotBaseBean<List<DouBanBeans>>> getImages(List<String> isbn) {
        return source.getImages(isbn);
    }

    public void saveConfig(ConfigBean bean) {
        source.saveConfig(bean);
    }
}
